import {
  getOrderDetail,
  payOrder,
  passOrder,
  addOrder,
  cancalOrder
} from "../../utils/config"
import store from "../../utils/factory"
const util = require('../../utils/util.js')

const app = getApp()
let that = null;

Page({
  data: {
    detail: {},
    expressThreshold: ""
  },
  onLoad: function (e) {
    console.log(e.id)
    that = this;
    this.getOrderDetail(e.id);
    this.setData({
      expressThreshold: store.get("expressThreshold")
    })
  },
  onShow() {

  },
  cancelOrder() {
    wx.showModal({
      title: "温馨提示",
      content: "是否确认取消订单",
      showCancel: true,
      success: function (res) {
        console.log(res)
        if (res.confirm) {
          wx.showLoading({
            title: "取消订单中..."
          })
          cancalOrder([that.data.detail.id]).then(res => {
            wx.hideLoading()
            wx.showToast({
              title: '取消订单成功',
              duration: 1000
            })
            // 在C页面内 navigateBack，将返回A页面
            wx.navigateBack({
              delta: 1
            })
          }).catch(err => {
            wx.hideLoading()
          })
        }
      }
    })
  },
  submit() {
    let submitOrderDetail = {
      shopCarList: that.data.detail.items,
      total: that.data.detail.total > store.get("expressThreshold") ? that.data.detail.total : that.data.detail.total + that.data.detail.express,
      status: that.data.detail.status,
      id: that.data.detail.id,
      haveExpress: that.data.detail.total > store.get("expressThreshold") ? false : true
    }
    store.set("submitOrderDetail", submitOrderDetail)
    wx.redirectTo({
      url: '../submitOrder/submitOrder'
    })
  },
  pass() {
    wx.showModal({
      title: "温馨提示",
      content: "是否确认收货",
      showCancel: true,
      success: function (res) {
        console.log(res)
        if (res.confirm) {
          passOrder({
            id: that.data.detail.id
          }).then(res => {
            wx.showToast({
              title: '确认收货成功',
              duration: 1000
            })
          })
        }
      }
    })
  },
  refund(e) {
    store.set("refundOrderDetail", this.data.detail)
    wx.redirectTo({
      url: '../refundOrder/refundOrder'
    })
  },
  getOrderDetail(id) {
    getOrderDetail({
      id: id
    }).then(res => {
      let req = res.data;
      req.createTime = util.formatTime(new Date(req.createTime));
      req.items.map(item => {
        item.total = (item.quantity * item.marketPrice).toFixed(1)
      })
      this.setData({
        detail: req
      })
    })
  }
})